package com.ruoyi.wechart.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.device.domain.DeviceUserRegister;
import com.ruoyi.device.service.IDeviceUserRegisterService;
import com.ruoyi.wechart.dmain.WxLoginUser;
import com.ruoyi.wechart.service.IWxLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 微信小程序登录controller
 * @author
 * @date
 */
@RestController
@RequestMapping("/wechart")
public class WxLoginController extends BaseController {
    @Autowired
    private IWxLoginService loginService;

    @Autowired
    private IDeviceUserRegisterService registerService;

    /**
     * 微信小程序登录
     * @param code 微信预发录code
     * @param userName 用户名
     * @param password 密码
     * @return
     */
    @PostMapping("/login")
    public AjaxResult wxCodeLogin(String code, String userName, String password) {
        WxLoginUser user = loginService.login(code, userName, password);
        return user.getStatus() >= 0 ? success(user) : error(user.getMsg());
    }

    /**
     * 注册新用户
     * @param user
     * @return
     */
    @PostMapping("/register")
    public AjaxResult wxRegister(@RequestBody DeviceUserRegister user) {
        List<DeviceUserRegister> list = registerService.getRegisterList(user);
        if (!list.isEmpty()) {
            return error("该用户已注册");
        }
        return toAjax(loginService.register(user));
    }
}
